package model

import (
	"time"
)

// FileType 文件类型枚举
type FileType string

const (
	FileTypeAvatar      FileType = "avatar"      // 头像
	FileTypeCertificate FileType = "certificate" // 证书
	FileTypeTraining    FileType = "training"    // 训练照片
	FileTypeOther       FileType = "other"       // 其他
)

// FileStatus 文件状态枚举
type FileStatus string

const (
	FileStatusNormal  FileStatus = "normal"  // 正常
	FileStatusDeleted FileStatus = "deleted" // 已删除
)

// StorageType 存储类型枚举
type StorageType string

const (
	StorageTypeLocal    StorageType = "local"    // 本地存储
	StorageTypeDatabase StorageType = "database" // 数据库存储(MySQL BLOB)
	StorageTypeOSS      StorageType = "oss"      // 阿里云OSS
	StorageTypeCOS      StorageType = "cos"      // 腾讯云COS
)

// FileRecord 文件记录表
type FileRecord struct {
	ID               uint        `gorm:"primaryKey" json:"id"`
	FileName         string      `gorm:"size:255;not null;comment:原始文件名" json:"fileName"`
	FileType         FileType    `gorm:"type:varchar(20);not null;index;comment:文件类型" json:"fileType"`
	FilePath         string      `gorm:"size:500;comment:文件路径" json:"filePath"`
	FileURL          string      `gorm:"size:500;comment:访问URL" json:"fileUrl"`
	FileContent      []byte      `gorm:"type:MEDIUMBLOB;comment:文件二进制内容(database模式)" json:"-"` // 数据库存储模式使用
	ThumbnailURL     string      `gorm:"size:500;comment:缩略图URL" json:"thumbnailUrl,omitempty"`
	ThumbnailContent []byte      `gorm:"type:BLOB;comment:缩略图二进制内容" json:"-"` // 缩略图存储`
	FileSize         int64       `gorm:"not null;comment:文件大小(字节)" json:"fileSize"`
	MimeType         string      `gorm:"size:100;not null;comment:MIME类型" json:"mimeType"`
	MD5              string      `gorm:"size:32;index;comment:文件MD5" json:"md5"`
	Width            int         `gorm:"comment:图片宽度" json:"width,omitempty"`
	Height           int         `gorm:"comment:图片高度" json:"height,omitempty"`
	StorageType      StorageType `gorm:"type:varchar(20);default:'local';comment:存储类型" json:"storageType"`
	UploaderID       uint        `gorm:"not null;index;comment:上传者ID" json:"uploaderId"`
	UploaderType     string      `gorm:"size:20;not null;comment:上传者类型(member/coach/admin)" json:"uploaderType"`
	Status           FileStatus  `gorm:"type:varchar(20);default:'normal';index;comment:状态" json:"status"`
	CreatedAt        time.Time   `gorm:"autoCreateTime" json:"createdAt"`
	UpdatedAt        time.Time   `gorm:"autoUpdateTime" json:"updatedAt"`
}

// TableName 指定表名
func (FileRecord) TableName() string {
	return "file_records"
}

// TrainingPhoto 训练照片表
type TrainingPhoto struct {
	ID           uint      `gorm:"primaryKey" json:"id"`
	MemberID     uint      `gorm:"not null;index;comment:会员ID" json:"memberId"`
	FileID       uint      `gorm:"not null;index;comment:文件记录ID" json:"fileId"`
	PhotoType    string    `gorm:"size:20;not null;comment:照片类型(before/after/progress)" json:"photoType"`
	TrainingDate time.Time `gorm:"type:date;index;comment:训练日期" json:"trainingDate"`
	Weight       float64   `gorm:"type:decimal(5,2);comment:体重(kg)" json:"weight,omitempty"`
	BodyFat      float64   `gorm:"type:decimal(5,2);comment:体脂率(%)" json:"bodyFat,omitempty"`
	Description  string    `gorm:"type:text;comment:描述" json:"description,omitempty"`
	IsPublic     bool      `gorm:"default:false;comment:是否公开" json:"isPublic"`
	LikesCount   int       `gorm:"default:0;comment:点赞数" json:"likesCount"`
	CreatedAt    time.Time `gorm:"autoCreateTime" json:"createdAt"`
	UpdatedAt    time.Time `gorm:"autoUpdateTime" json:"updatedAt"`

	// 关联
	Member *Member     `gorm:"foreignKey:MemberID" json:"member,omitempty"`
	File   *FileRecord `gorm:"foreignKey:FileID" json:"file,omitempty"`
}

// TableName 指定表名
func (TrainingPhoto) TableName() string {
	return "training_photos"
}

// CoachCertificate 教练证书表
type CoachCertificate struct {
	ID              uint       `gorm:"primaryKey" json:"id"`
	CoachID         uint       `gorm:"not null;index;comment:教练ID" json:"coachId"`
	FileID          uint       `gorm:"not null;index;comment:文件记录ID" json:"fileId"`
	CertificateName string     `gorm:"size:100;not null;comment:证书名称" json:"certificateName"`
	CertificateNo   string     `gorm:"size:100;comment:证书编号" json:"certificateNo,omitempty"`
	IssuingOrg      string     `gorm:"size:200;comment:颁发机构" json:"issuingOrg,omitempty"`
	IssueDate       time.Time  `gorm:"type:date;comment:颁发日期" json:"issueDate,omitempty"`
	ExpiryDate      time.Time  `gorm:"type:date;comment:到期日期" json:"expiryDate,omitempty"`
	IsVerified      bool       `gorm:"default:false;comment:是否已验证" json:"isVerified"`
	VerifiedAt      *time.Time `gorm:"comment:验证时间" json:"verifiedAt,omitempty"`
	VerifiedBy      *uint      `gorm:"comment:验证人ID" json:"verifiedBy,omitempty"`
	Remark          string     `gorm:"type:text;comment:备注" json:"remark,omitempty"`
	CreatedAt       time.Time  `gorm:"autoCreateTime" json:"createdAt"`
	UpdatedAt       time.Time  `gorm:"autoUpdateTime" json:"updatedAt"`

	// 关联
	Coach *Coach      `gorm:"foreignKey:CoachID" json:"coach,omitempty"`
	File  *FileRecord `gorm:"foreignKey:FileID" json:"file,omitempty"`
}

// TableName 指定表名
func (CoachCertificate) TableName() string {
	return "coach_certificates"
}

// ======================== 请求/响应模型 ========================

// UploadFileReq 文件上传请求
type UploadFileReq struct {
	FileType     FileType `form:"fileType" binding:"required,oneof=avatar certificate training other"`
	UploaderID   uint     `form:"uploaderId" binding:"required"`
	UploaderType string   `form:"uploaderType" binding:"required,oneof=member coach admin"`
	// 文件通过 multipart/form-data 上传，在 Handler 中处理
}

// UploadFileResp 文件上传响应
type UploadFileResp struct {
	FileID       uint   `json:"fileId"`
	FileName     string `json:"fileName"`
	FileURL      string `json:"fileUrl"`
	ThumbnailURL string `json:"thumbnailUrl,omitempty"`
	FileSize     int64  `json:"fileSize"`
	FileType     string `json:"fileType"`
	UploadedAt   string `json:"uploadedAt"`
}

// AddTrainingPhotoReq 添加训练照片请求
type AddTrainingPhotoReq struct {
	MemberID     uint    `json:"memberId" binding:"required"`
	FileID       uint    `json:"fileId" binding:"required"`
	PhotoType    string  `json:"photoType" binding:"required,oneof=before after progress"`
	TrainingDate string  `json:"trainingDate" binding:"required"` // YYYY-MM-DD
	Weight       float64 `json:"weight"`
	BodyFat      float64 `json:"bodyFat"`
	Description  string  `json:"description"`
	IsPublic     bool    `json:"isPublic"`
}

// AddCertificateReq 添加教练证书请求
type AddCertificateReq struct {
	CoachID         uint   `json:"coachId" binding:"required"`
	FileID          uint   `json:"fileId" binding:"required"`
	CertificateName string `json:"certificateName" binding:"required"`
	CertificateNo   string `json:"certificateNo"`
	IssuingOrg      string `json:"issuingOrg"`
	IssueDate       string `json:"issueDate"`  // YYYY-MM-DD
	ExpiryDate      string `json:"expiryDate"` // YYYY-MM-DD
	Remark          string `json:"remark"`
}

// VerifyCertificateReq 验证证书请求
type VerifyCertificateReq struct {
	IsVerified bool   `json:"isVerified" binding:"required"`
	Remark     string `json:"remark"`
}

// FileListReq 文件列表查询请求
type FileListReq struct {
	FileType     string `form:"fileType"`
	UploaderID   uint   `form:"uploaderId"`
	UploaderType string `form:"uploaderType"`
	Status       string `form:"status"`
	StartDate    string `form:"startDate"` // YYYY-MM-DD
	EndDate      string `form:"endDate"`   // YYYY-MM-DD
	Page         int    `form:"page" binding:"required,min=1"`
	PageSize     int    `form:"pageSize" binding:"required,min=1,max=100"`
}

// FileListResp 文件列表响应
type FileListResp struct {
	Total int          `json:"total"`
	Page  int          `json:"page"`
	Size  int          `json:"size"`
	List  []FileRecord `json:"list"`
}

// TrainingPhotoListReq 训练照片列表查询请求
type TrainingPhotoListReq struct {
	MemberID  uint   `form:"memberId"`
	PhotoType string `form:"photoType"`
	StartDate string `form:"startDate"` // YYYY-MM-DD
	EndDate   string `form:"endDate"`   // YYYY-MM-DD
	IsPublic  *bool  `form:"isPublic"`
	Page      int    `form:"page" binding:"required,min=1"`
	PageSize  int    `form:"pageSize" binding:"required,min=1,max=100"`
}

// TrainingPhotoListResp 训练照片列表响应
type TrainingPhotoListResp struct {
	Total int             `json:"total"`
	Page  int             `json:"page"`
	Size  int             `json:"size"`
	List  []TrainingPhoto `json:"list"`
}

// CertificateListReq 证书列表查询请求
type CertificateListReq struct {
	CoachID    uint  `form:"coachId"`
	IsVerified *bool `form:"isVerified"`
	Page       int   `form:"page" binding:"required,min=1"`
	PageSize   int   `form:"pageSize" binding:"required,min=1,max=100"`
}

// CertificateListResp 证书列表响应
type CertificateListResp struct {
	Total int                `json:"total"`
	Page  int                `json:"page"`
	Size  int                `json:"size"`
	List  []CoachCertificate `json:"list"`
}

// FileStatistics 文件统计
type FileStatistics struct {
	TotalFiles       int64  `json:"totalFiles"`
	TotalSize        int64  `json:"totalSize"`    // 字节
	TotalSizeStr     string `json:"totalSizeStr"` // 格式化显示
	AvatarCount      int64  `json:"avatarCount"`
	CertificateCount int64  `json:"certificateCount"`
	TrainingCount    int64  `json:"trainingCount"`
	OtherCount       int64  `json:"otherCount"`
}
